﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Linq;
using System.Text;
using WMSADBUtility;
using WMSAML.SY;

namespace WMSADL.SY
{
    public class TDictionary_SortDAL
    {
        /// <summary>
        /// 
        /// </summary>
        public int Add_Dictionary_Sort(TDictionary_SortEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TDictionary_Sort(");
            strSql.Append("Value)");
            strSql.Append(" values (");
            strSql.Append("@Value)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Value", DbType.String, entity.Value);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }

        /// <summary>
        /// ByID
        /// </summary>
        /// <summary>
        public TDictionary_SortEntity GetEntityByID_Dictionary_Sort(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ID,Value ");
            strSql.Append("FROM TDictionary_Sort ");
            strSql.Append("WHERE ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, id);
                TDictionary_SortEntity obj = new TDictionary_SortEntity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        obj.ID = Convert.ToInt32(dataReader["ID"].ToString());
                        obj.Value = dataReader["Value"].ToString();
                    }
                    else
                    {
                        obj = null;
                    }
                }
                return obj;
            }
        }

        /// <summary>
        /// ByName
        /// </summary>
        /// <summary>
        public TDictionary_SortEntity GetEntityByName_Dictionary_Sort(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ID,Value ");
            strSql.Append("FROM TDictionary_Sort ");
            strSql.Append("WHERE Value=@Value ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "Value", DbType.String, name);
                TDictionary_SortEntity obj = new TDictionary_SortEntity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        obj.ID = Convert.ToInt32(dataReader["ID"].ToString());
                        obj.Value = dataReader["Value"].ToString();
                    }
                    else
                    {
                        obj = null;
                    }
                }
                return obj;
            }
        }

        /// <summary>
        ///  删除
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>bool</returns>
        public bool Del_Dictionary_Sort(int id)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                StringBuilder strSQL = new StringBuilder();
                strSQL.Append("DELETE FROM TDictionary_Sort WHERE ID=@ID ");
                using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL.ToString()))
                {
                    db.AddInParameter(dbCommand, "ID", DbType.String, id);

                    return (db.ExecuteNonQuery(dbCommand) < 1) ? false : true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        ///  修改信息
        /// </summary>
        /// <param name="entity">entity</param>
        /// <returns>bool</returns>
        public bool Update_Dictionary_Sort(TDictionary_SortEntity entity)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE TDictionary_Sort SET Value=@Value WHERE ID=@ID ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, entity.ID);
                db.AddInParameter(dbCommand, "Value", DbType.String, entity.Value);
                return (db.ExecuteNonQuery(dbCommand) < 1) ? false : true;
            }
        }
    }
}
